<?php
namespace app\admin\controller;
use app\admin\model\Wxapp as WxappModel;
use think\Cache;

/**
 * 商户管理控制器
 */
class Wxapp extends Controller
{
    /**
     * 商户列表
     */
    public function index()
    {
        $model = new WxappModel;
        $list = $model->getList();
        return $this->fetch('index', compact('list'));
    }

    /**
     * 删除
     */
    public function delete($wxapp_id)
    {
		return $this->renderError('暂不支持删除');
        $model = WxappModel::getWxapp($wxapp_id);
        if (!$model->remove()) {
            return $this->renderError('删除失败');
        }
        return $this->renderSuccess('删除成功');
    }
	
	/**
     * 自定义小程序版权开关
     */
    public function copyright($wxapp_id)
    {
		//测试判断
		if($this->admin['user']['config_id']==0){
			return $this->error('测试用户不可操作');
		}
        //详情
        $model = WxappModel::getWxapp(['wxapp_id' => $wxapp_id]);
		$model->is_copyright['value']==0 ? $model->is_copyright = 1 : $model->is_copyright = 0;
		$model->save();
		Cache::rm('wxapp_' . $wxapp_id);
        return $this->renderSuccess('更新成功', url('wxapp/index'));
    }
	
	/**
     * 管理员执行小程序登录
     */
    public function appLogin($wxapp_id)
    {	
		//测试判断
		if($this->admin['user']['config_id']==0){
			return $this->error('测试用户不可操作');
		}
		$model = WxappModel::getWxapp(['wxapp_id' => $wxapp_id]);
        if ($model->appLogin()) {
            $this->redirect('/store.php');
        }
        $this->error('登录错误', '/store.php');
    }


}
